package com.cat.model.biz.service.admin.impl;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.core.commons.tuple.TwoTuple;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.admin.IndexRecomService;
import com.cat.model.common.model.common.IndexRecomVO;
import com.cat.model.common.util.FileUtil;
import com.cat.model.common.util.StaticFileUtil;

@Service("IndexRecomService")
public class IndexRecomServiceImpl implements IndexRecomService{
	@Autowired
	private TxCommonDAO commonDAO;
	
	@Override
	public IndexRecomVO createIndexRecom(IndexRecomVO indexRecom, File image,
			String imageFileName){
		if(image != null){
			String imagePath = StaticFileUtil.indexRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			indexRecom.setImage(imagePath + "/" + imageFileName);
		}
		
		commonDAO.insert(indexRecom);
		
		return indexRecom;
	}
	
	@Override
	public int deleteIndexRecom(long id){
		IndexRecomVO indexRecom = new IndexRecomVO();
		indexRecom.setId(id);
		return commonDAO.deleteById(indexRecom);
	}
	
	@Override
	public int updateIndexRecom(IndexRecomVO indexRecom, File image,
			String imageFileName){
		if(image != null){
			String imagePath = StaticFileUtil.indexRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			indexRecom.setImage(imagePath + "/" + imageFileName);
		}
		return commonDAO.updateById(indexRecom, "cityCode", "image","orderIndex","modelId","arriveStartTime","arriveEndTime");
	}
}
